<template>
  <div class="app-container calendar-list-container">
      <el-table
      :key="tableKey"
      :data="list"
      v-loading.body="listLoading"
      border
      fit
      highlight-current-row
      style="width: 99%;overflow: auto; margin-top: 10px;"
    >
      <el-table-column align="center" label="排序" min-width="10%">
        <template scope="scope">
          <span>{{scope.row.orderLevel}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="30%" align="center" label="推荐分组">
         <template scope="scope">
          <span>{{scope.row.groupName}}</span>
        </template>
      </el-table-column>
       <el-table-column align="center" label="操作" min-width="20%">
         <template scope="scope">
           <el-button
            v-if="goodsManager_btn_edit"
            size="small"
            type="success"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
        </template> 
      </el-table-column> 
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination
         @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="listQuery.pageNo"
        :page-sizes="[10,20,30, 50]"
        :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>

    <el-dialog title="编辑分组" :visible.sync="dialogFormVisible">
  <el-form :model="form">
    <el-form-item label="排序" :label-width="formLabelWidth" >
      <el-input v-model="form.orderLevel" disabled></el-input>
    </el-form-item>
    <el-form-item label="推荐分组*" :label-width="formLabelWidth">
      <el-select v-model="form.groupId" clearable filterable placeholder="请选择分组">
        <el-option v-for="item in regionVal"
          :key="item.groupId"
          :label="item.groupName"
          :value="item.groupId"
        ></el-option>
       
      </el-select>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="updateGroup">确 定</el-button>
  </div>
</el-dialog>
    </div>
  </div>
</template>

<script>

import { mapGetters } from "vuex";
import { getToken } from "utils/auth";
import globalVariable from '@/utils/global'
import { groupList,getGroupStatus,updateGroupRecomm } from "api/shop/goods/group/index";
export default {
  name: "bannerAdManage",
  components:{
    
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      listLoading: true,
      total: null,
      goodsManager_btn_edit: false,
      dialogFormVisible:false,
       listQuery: {
        pageNo: 1,
        pageSize: 20,
        shopId:undefined
      },
      regionVal:[],
      formLabelWidth: '120px',
      form:{
        recommId:undefined,
        groupId:undefined,
        recommStatus:undefined,
        orderLevel:undefined,
        shopId:undefined,
      }
    };
  },
  created() {
    this.getGroupRecommInfo();
    this.getGroup();
     this.goodsManager_btn_edit = this.elements["goodsManager:btn_edit"];
  },
  computed: {
    ...mapGetters(["elements"])
  },
  methods: {
    getGroupRecommInfo(){
       this.listQuery.shopId=globalVariable.shopId;
       this.listLoading = true;
      groupList(this.listQuery).then(response=>{
        this.list = response.data.rows;
        this.total = response.data.total;
        this.listLoading = false;
      })
    },
    getGroup(){
      getGroupStatus(globalVariable.shopId).then(response=>{
        this.regionVal=response.data;
      })
    }
    ,
     handleSizeChange(val) {
      this.listQuery.pageSize = val;
        this.getGroupRecommInfo();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNo = val;
        this.getGroupRecommInfo();
    },
    handleUpdate(row){
     this.dialogFormVisible=true;
     this.getGroup();
     this.form.recommId=row.recommId;
     this.form.orderLevel=row.orderLevel;
     this.form.groupId=row.groupId;
     this.form.shopId=row.shopId;
    },
    updateGroup(){
      if(this.form.groupId=="" || this.form.groupId==null){
         this.$message.error("推荐分组不能为空");
        return false;
      }
      updateGroupRecomm(this.form).then(response=>{
       if(response.message==200){
           this.$notify({
                title: "成功",
                message: "分组修改成功!",
                type: "success",
                duration: 2000
              });
          this.dialogFormVisible=false;
          this.getGroupRecommInfo();
       }
      })
    
    }
  }
};
</script>
<style>

</style>
